﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using LiSAClient.Core;
using LiSAClient.Core.Interfaces;
using LiSAClient.Core.Messages;
using LiSAClient.Core.Models;
using LiSAClient.Core.Services;
using LiSAClient.Core.Services.Factories;
using LiSAClient.Core.Viewmodel;
using LiSAClient.LiSAService;

namespace LiSAClient.WpfClient.ViewModel.Projects
{
    public class NewProject_Step7ViewModel: ContentViewModel, ICommitable
    {
        public ICommand Commit { get; private set; }

        public ICommand PreviousStep { get; private set; }

        public ICommand AddUser { get; private set; }

        private List<User> usersInSystem;
        public List<User> UsersInSystem
        {
            get
            {
                return this.usersInSystem;
            }

            set
            {
                this.Set(() => this.UsersInSystem, ref this.usersInSystem, value);
            }
        }

        private User selectedUser;
        public User SelectedUser
        {
            get
            {
                return this.selectedUser;
            }

            set
            {
                this.Set(() => this.SelectedUser, ref this.selectedUser, value);
            }
        }

        private User newUser;
        public User NewUser
        {
            get
            {
                return this.newUser;
            }

            set
            {
                this.Set(() => this.NewUser, ref this.newUser, value);
            }
        }

        private Project project;
        public Project Project
        {
            get
            {
                return this.project;
            }

            set
            {
                this.Set(() => this.Project, ref this.project, value);
            }
        }

        #region Initializers
        public NewProject_Step7ViewModel(Project proj)
        {
            this.Project = proj;
            this.Commit = new RelayCommand(SaveProject);
            this.PreviousStep = new RelayCommand(DoPreviousStep);
            this.AddUser = new RelayCommand(DoAddUser);
            this.SelectedUser = new User();
            this.UsersInSystem = new List<User>();
        }

        public override void OnPresented()
        {
            this.Presenter.StartWaiting("Fetching users...");
            Task.Factory.StartNew(
                () =>
                {
                    if(this.UsersInSystem == null) this.UsersInSystem = new List<User>();
                    var theusers = ServiceFactory.Instance.Get<UserService>().GetAllUsers();
                    foreach (var aUSer in theusers)
                    {
                        this.UsersInSystem.Add(aUSer);
                    }

                    // No point in lsiting the current user since it will be added upon creation anyway by the service
                    if (UsersInSystem.Any(x => x.UserName == AppContext.CurrentContext.User.UserName))
                    {
                        UsersInSystem.Remove(
                            UsersInSystem.SingleOrDefault(x => x.UserName == AppContext.CurrentContext.User.UserName));
                    }
                    this.Presenter.FinishedWaiting();
                });
           
        }
        #endregion

        #region methods
        /// <summary>
        /// Move to the previous step in the wizard
        /// </summary>
        public void DoPreviousStep()
        {
            this.Presenter.PresentContent(new NewProject_Step6ViewModel(this.Project));
        }
        /// <summary>
        /// Move to the next step in the wizard
        /// </summary>
        public void DoAddUser()
        {
            if (this.SelectedUser != null && !string.IsNullOrWhiteSpace(this.SelectedUser.FullName))
            {
                var newUsers = new List<User>(this.Project.Users) {this.SelectedUser};
                this.Project.Users = newUsers;
                this.UsersInSystem.Remove(this.SelectedUser);
            }
        }

        /// <summary>
        /// Save the project and close the window
        /// </summary>
        public void SaveProject()
        {
            // Ensure legality
            if (string.IsNullOrWhiteSpace(this.Project.Title))
            {
                Messenger.Default.Send(new ShowNotificationMessage
                {
                    NotificationType = NotificationType.Warning,
                    Header = "Warning!",
                    Body = "The project must have a title."
                });
                return;
            }
            if (string.IsNullOrWhiteSpace(this.Project.Description))
            {
                Messenger.Default.Send(new ShowNotificationMessage
                {
                    NotificationType = NotificationType.Warning,
                    Header = "Warning!",
                    Body = "The project must have a description."
                });
                return;
            }

            this.Project.ProjectLead = AppContext.CurrentContext.User.ID;
            this.Project.CurrentPhase = 0;

            ServiceFactory.Instance.Get<ProjectService>().CreateNewProject(this.Project);

            // Close the window
            if (this.Presenter != null)
            {
                this.Presenter.SendClosingRequest();
            }
        }
        #endregion
    }
}